package com.whfc.ms.api.hdc.controller;

import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.fuum.entity.SysUser;
import com.whfc.hdc.dto.FshDeviceDataDTO;
import com.whfc.hdc.dto.FshDeviceMediaDTO;
import com.whfc.hdc.dto.FshStrRoomIdDTO;
import com.whfc.hdc.params.FshDeviceControlParam;
import com.whfc.hdc.params.FshDeviceInitParam;
import com.whfc.hdc.params.FshEnterRoomParam;
import com.whfc.ms.api.hdc.service.MsFshService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;

/**
 * @author qzexing
 * @version 1.0
 * @date 2021-04-09 15:18
 */
@RequiresPermissions("hdc/fvs")
@RestController
@RequestMapping("/ms/api/hdc/fsh")
public class FshDeviceController {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private MsFshService msFshService;


    @GetMapping("/list")
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestParam("pageNum") Integer pageNum,
                       @RequestParam("pageSize") Integer pageSize,
                       @RequestParam(value = "keyword", required = false) String keyword) {
        logger.info("获取视频安全帽列表, loginer:{}, pageNum:{}, pageSize:{}, keyword:{}"
                , user.getUsername(), pageNum, pageSize, keyword);
        PageData<FshDeviceDataDTO> pageData = msFshService.list(pageNum, pageSize, keyword);
        return ResultUtil.success(pageData);
    }


    @PostMapping("/control")
    public Result control(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                          @RequestBody @Validated FshDeviceControlParam params) {
        logger.info("控制视频安全帽设备, loginer:{}, params:{}", user.getUsername(), params);
        msFshService.control(params);
        return ResultUtil.success();
    }

    @GetMapping("/data/log")
    public Result dataLog(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                          @RequestParam("sn") String sn,
                          @RequestParam("pageNum") Integer pageNum,
                          @RequestParam("pageSize") Integer pageSize,
                          @RequestParam("startTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
                          @RequestParam("endTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime) {
        logger.info("获取视频安全帽历史数据, loginer:{}, sn:{}, pageNum:{}, pageSize:{}, startTime:{}, endTime:{}",
                user.getUsername(), sn, pageNum, pageSize, startTime, endTime);
        PageData<FshDeviceDataDTO> pageData = msFshService.getDataLog(sn, pageNum, pageSize, startTime, endTime);
        return ResultUtil.success(pageData);
    }

    @GetMapping("/media/list")
    public Result mediaList(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                            @RequestParam("sn") String sn,
                            @RequestParam("pageNum") Integer pageNum,
                            @RequestParam("pageSize") Integer pageSize,
                            @RequestParam("startTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startTime,
                            @RequestParam("endTime") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date endTime,
                            @RequestParam("mediaTypes") List<Integer> mediaTypes,
                            @RequestParam("sources") List<Integer> sources) {
        logger.info("获取视频安全帽多媒体数据, loginer:{}, sn:{}, pageNum:{}, pageSize:{}, startTime:{}, endTime:{}, " +
                "mediaTypes:{}, sources:{}", user.getUsername(), sn, pageNum, pageSize, startTime, endTime, mediaTypes, sources);
        PageData<FshDeviceMediaDTO> pageData = msFshService
                .getMediaList(sn, pageNum, pageSize, startTime, endTime, mediaTypes, sources);
        return ResultUtil.success(pageData);
    }


    @PostMapping("/upload")
    public Result upload(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                         @RequestParam("sn") String sn,
                         @RequestParam("mediaType") Integer mediaType,
                         @RequestParam("file") MultipartFile file) {
        logger.info("视频安全帽上传多媒体文件, loginer:{}, sn:{}, mediaType:{}, file:{}"
                , user.getUsername(), sn, mediaType, file.getOriginalFilename());
        msFshService.upload(sn, mediaType, file);
        return ResultUtil.success();
    }


    @PostMapping("/device/init")
    public Result initDevice(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                             @Validated @RequestBody FshDeviceInitParam params) {
        logger.info("视频安全帽初始化设备数据, loginer:{}, params:{}", user.getUsername(), params);
        msFshService.initDeviceConfig(params);
        return ResultUtil.success();
    }


    @PostMapping("/enter-room")
    public Result enterRoom(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                            @RequestBody FshEnterRoomParam params) {
        logger.info("发起通话, loginer:{}, params:{}", user.getUsername(), params);
        FshStrRoomIdDTO strRoomIdDTO = msFshService.enterRoom(params);
        return ResultUtil.success(strRoomIdDTO);
    }


}
